#ifndef _IM_SERVER_CONNECTION_MANAGER_
#define _IM_SERVER_CONNECTION_MANAGER_

#include "ConnectionManagerService_types.h"
#include "ConnectionManagerService.h"

#include "redis_executor/simple_redis_locator.h"
#include "redis_executor/redis_executor.h"
#include "im/common/common.h"
#include "base/logging.h"
#include "melon/service/service_arch.h"
#include "melon/service/base_service_impl.h"
#include "base/zk_client.h"
#include "base/singleton.h"

using namespace std;

namespace imserver {

class UidConnServerMap : public base::Singleton<UidConnServerMap> {
 public:
  void set(int64_t uid, const std::string& server_key);
  void setValid(int64_t uid, const std::string& server_key);
  std::string alloc(int64_t uid, const std::string& conn_type);
  void remove(int64_t uid, const std::string& conn_type);
  std::string get(int64_t uid, const std::string& conn_type);

 private:
  friend class base::Singleton<UidConnServerMap>;

  UidConnServerMap() :
      zk_client_(ConfigReader("../conf/zookeeper.conf").Get("zookeeper", "service_registry").c_str()),
      redisExecutor(new FixedLocator("127.0.0.1:6379")) {
    zk_client_.Init();
  }

  base::ZookeeperClient zk_client_;
  RedisExecutor redisExecutor;
};

class ConnectionManagerServiceHandler : public ConnectionManagerServiceIf, public melon::service::BaseServiceImpl {
public:
  ConnectionManagerServiceHandler() {}
  virtual void GetCometServiceKeyByUids(std::map<int64_t, std::string> & _return, const std::set<int64_t> & uids);
  virtual void RemoveCometServiceKeyByUids(const std::set<int64_t>& uids);
  virtual void AllocateCometServiceForUid(std::string& _return, const int64_t uid);
  virtual void SetValidCometServiceForUid(int64_t uid, const std::string& server_key);

  virtual void GetSockServiceKeyByUids(std::map<int64_t, std::string> & _return, const std::set<int64_t> & uids);
  virtual void RemoveSockServiceKeyByUids(const std::set<int64_t>& uids);
  virtual void AllocateSockServiceForUid(std::string& _return, const int64_t uid);
  virtual void SetValidSockServiceForUid(int64_t uid, const std::string& server_key);
};

}

#endif // _IM_SERVER_CONNECTION_MANAGER_
